<?php
namespace app\index\controller;

use app\common\Base;
use app\index\model\UserAddress;
use app\index\validate\UserAddressValidator;
use think\Exception;
use think\Request;

class ReceivingAddress extends Base
{
    /**
     * @param UserAddress $userAddress
     * @return \think\response\Json
     * @throws Exception
     */
    //显示用户收货地址的信息
    public function findUserAdd(Request $request,UserAddress $userAddress){
        try{
            // TODO 将获取缓存信息放在中间件里
            //获取请求头
            $arr = $request->cacheInfo;

            //找出地址信息
            if (!($addInfo = $userAddress->findUserAddressInfo($arr['id']))) {
                return responseJson(Base::FAIL,'收货地址数据返回失败');
            }

            return responseJson(Base::OK,'收货地址数据返回成功',$addInfo);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @param Request $request
     * @param UserAddress $userAddress
     * @return \think\response\Json
     * @throws Exception
     */
    //添加收货地址
    //传值,$provide,$city,$county,$street,$detail,$name,$sex,$phone
    public function addUserAddress(Request $request,UserAddress $userAddress,UserAddressValidator $userAddressValidator){
        try{
            if (!$userAddressValidator->check($request->post())){
                return responseJson(Base::FAIL,$userAddressValidator->getError());
            }
            // TODO 将获取缓存信息放在中间件里
            //获取请求头
            $arr = $request->cacheInfo;

            //获取前端传的值
            //省
            $provide = $request->post('provide');
            //城市
            $city = $request->post('city');
            //区
            $county = $request->post('county');
            //街道
            $street = $request->post('street');
            //地址
            $detail = $request->post('detail');
            //联系人
            $name = $request->post('name');
            //性别
            $sex = $request->post('sex');
            //手机号
            $phone = $request->post('phone');

            //传data
            $data = $userAddress->insertAddress($arr['id'],$provide,$city,$county,$street,$detail,$name,$sex,$phone);

            //判断
            if ($data != 1){
                return responseJson(Base::FAIL,'新增收货地址失败');
            }
            return responseJson(Base::OK,'新增收货地址成功');
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @param Request $request
     * @param UserAddress $userAddress
     * @return \think\response\Json
     */
    //修改用户地址
    //传值,$addId,$provide,$city,$county,$street,$detail,$name,$sex,$phone
    public function updateUserAddress(Request $request,UserAddress $userAddress){
        // TODO 将获取缓存信息放在中间件里
        try{
            //获取请求头
            $arr = $request->cacheInfo;
            //获取前端返回的地址id
            $addId = $request->post('addId');
            //省
            $provide = $request->post('provide');
            //城市
            $city = $request->post('city');
            //区
            $county = $request->post('county');
            //街道
            $street = $request->post('street');
            //地址
            $detail = $request->post('detail');
            //联系人
            $name = $request->post('name');
            //性别
            $sex = $request->post('sex');
            //手机号
            $phone = $request->post('phone');

            $data = $userAddress->updateAddress($addId,$arr['id'],$provide,$city,$county,$street,$detail,$name,$sex,$phone);

            if ($data != 1){
                return responseJson(Base::FAIL,'更改地址失败');
            }
            return responseJson(Base::OK,'更改地址成功');
        }catch (Exception $e){
            throw $e;
        }
    }
}